package com.tanhua.dubbo.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.dto.TestsoulAnswersDto;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2021/11/10 0:12
 */
@DubboService
public class TestSoulApiImpl implements TestSoulApi{
    @Autowired
    private TestsoulPaperMapper testsoulPaperMapper;
    @Autowired
    private TestsoulQuestionsMapper testsoulQuestionsMapper;
    @Autowired
    private TestsoulQuestionOptionsMapper testsoulQuestionOptionsMapper;
    @Autowired
    private TestsoulResultDescMapper testsoulResultDescMapper;
    @Autowired
    private TestsoulResultMapper testsoulResultMapper;

    @Override
    public List<TestsoulPaper> paperList() {
        return testsoulPaperMapper.paperList();
    }

    @Override
    public TestsoulResult getTestsoulResultByAnswers(List<TestsoulAnswersDto> answerList, Long userId) {
        //获取本套试题的其中一个试题id
        Long questionId = Long.valueOf(answerList.get(0).getQuestionId());
        //根据试题id查询试题对象
        TestsoulQuestions question = testsoulQuestionsMapper.selectById(questionId);
        //根据问卷id查询计分规则
        LambdaQueryWrapper<TestsoulResultDesc> resultDescQueryWrapper = new LambdaQueryWrapper<>();
        Long paperId = question.getPaperId();
        resultDescQueryWrapper.eq(TestsoulResultDesc::getPaperId,paperId);
        List<TestsoulResultDesc> resultDescs = testsoulResultDescMapper.selectList(resultDescQueryWrapper);
        //循环答案列表，得到选项列表id
        List<Long> optionIds = answerList.stream().map(a -> Long.valueOf(a.getOptionId())).collect(Collectors.toList());
        //根据选项id列表查询选项集合,得到答题总分
        List<TestsoulQuestionOptions> options = testsoulQuestionOptionsMapper.selectBatchIds(optionIds);
        Long totalScore = options.stream().mapToInt(o -> o.getScore()).summaryStatistics().getSum();

        //循环计分规则，判断所得总分属于哪个结果
        TestsoulResultDesc resultDesc = null;
        for(TestsoulResultDesc resultDescTemp:resultDescs){
            if(totalScore >= resultDescTemp.getStartScore() && totalScore < resultDescTemp.getEndScore()){
                resultDesc = resultDescTemp;
                break;
            }
        }
        if(ObjectUtil.isEmpty(resultDesc)){
            return null;
        }
        //保存测试结果
        LambdaQueryWrapper<TestsoulResult> testsoulResultWrapper = new LambdaQueryWrapper<>();
        testsoulResultWrapper.eq(TestsoulResult::getUserId,userId).eq(TestsoulResult::getPaperId,paperId);
        TestsoulResult testsoulResult = testsoulResultMapper.selectOne(testsoulResultWrapper);
        if(ObjectUtil.isEmpty(testsoulResult)){
            testsoulResult = new TestsoulResult();
            testsoulResult.setResultId(resultDesc.getId());
            testsoulResult.setPaperId(resultDesc.getPaperId());
            testsoulResult.setUserId(userId);
            testsoulResultMapper.insert(testsoulResult);
        }else {
            testsoulResult.setResultId(resultDesc.getId());
            testsoulResultMapper.updateById(testsoulResult);
        }
        //将该套试题以及下一套试题ID放入该用户的redis解锁列表
        TestsoulPaper paper = this.testsoulPaperMapper.selectById(question.getPaperId());
        testsoulResult.setNextPaperId(paper.getNextPaperId());
        return testsoulResult;
    }

    @Override
    public List<TestsoulResult> getTestsoulResultByUserId(Long userId) {
        LambdaQueryWrapper<TestsoulResult> testsoulResultWrapper = new LambdaQueryWrapper<>();
        testsoulResultWrapper.eq(TestsoulResult::getUserId,userId);
        List<TestsoulResult> testsoulResults = this.testsoulResultMapper.selectList(testsoulResultWrapper);
        return testsoulResults;
    }

    @Override
    public TestsoulResultDesc getTestsoulResultDescById(String id) {
        TestsoulResultDesc resultDesc = testsoulResultDescMapper.selectById(id);
        return resultDesc;
    }
}
